%% 创建10kV星接级联H桥SVG主电路模型
function create_svg_star_model()
    % 初始化参数
    SVG_Star_10kV_System_Init;
    
    % 创建新模型
    model_name = 'SVG_Star_10kV_System';
    if bdIsLoaded(model_name)
        close_system(model_name, 0);
    end
    new_system(model_name);
    open_system(model_name);
    
    %% 1. 创建电网和主电路切换系统
    % 强电网源
    add_block('sps_lib/Sources/Three-Phase Source', [model_name '/Strong_Grid']);
    set_param([model_name '/Strong_Grid'], 'Position', [50 100 100 150]);
    set_param([model_name '/Strong_Grid'], 'Voltage', num2str(Vgrid_LL));
    set_param([model_name '/Strong_Grid'], 'Frequency', num2str(f_grid));
    set_param([model_name '/Strong_Grid'], 'InternalConnection', 'Yg');
    
    % 弱电网源（光伏配电网）
    add_block('sps_lib/Sources/Three-Phase Source', [model_name '/Weak_Grid_PV']);
    set_param([model_name '/Weak_Grid_PV'], 'Position', [50 200 100 250]);
    set_param([model_name '/Weak_Grid_PV'], 'Voltage', num2str(Vgrid_LL));
    set_param([model_name '/Weak_Grid_PV'], 'Frequency', num2str(f_grid));
    set_param([model_name '/Weak_Grid_PV'], 'InternalConnection', 'Yg');
    
    % 电网阻抗
    add_block('sps_lib/Passives/Three-Phase Series RLC Branch', [model_name '/Grid_Impedance']);
    set_param([model_name '/Grid_Impedance'], 'Position', [150 120 200 180]);
    set_param([model_name '/Grid_Impedance'], 'BranchType', 'RL');
    
    % 电网切换开关
    add_block('sps_lib/Power Electronics/Three-Phase Breaker', [model_name '/Grid_Switch']);
    set_param([model_name '/Grid_Switch'], 'Position', [250 120 300 180]);
    
    %% 2. 创建星接级联H桥主电路
    % 创建三相星接级联H桥
    create_star_cascaded_hbridge(model_name);
    
    %% 3. 创建储能系统（超级电容+隔离DC/DC）
    create_energy_storage_system(model_name);
    
    %% 4. 创建大型汽车厂负载
    create_automotive_factory_load(model_name);
    
    %% 5. 创建测量系统
    create_measurement_system(model_name);
    
    %% 6. 创建控制系统
    create_control_system(model_name);
    
    %% 7. 创建SCR检测和显示
    create_scr_detection(model_name);
    
    % 保存模型
    save_system(model_name);
    disp(['模型 ' model_name ' 创建完成！']);
end

%% 子函数：创建星接级联H桥
function create_star_cascaded_hbridge(model_name)
    load('SVG_Parameters.mat');
    
    % 创建子系统
    add_block('built-in/Subsystem', [model_name '/Star_Cascaded_HBridge']);
    set_param([model_name '/Star_Cascaded_HBridge'], 'Position', [400 100 600 300]);
    
    % 进入子系统
    subsys = [model_name '/Star_Cascaded_HBridge'];
    
    % 创建三相端口
    phases = {'A', 'B', 'C'};
    for i = 1:3
        % 输入端口
        add_block('built-in/Inport', [subsys '/Phase_' phases{i} '_in']);
        set_param([subsys '/Phase_' phases{i} '_in'], 'Position', [50 50+100*(i-1) 80 80+100*(i-1)]);
        
        % 创建级联H桥链
        for j = 1:N_bridge
            hb_name = [subsys '/HB_' phases{i} num2str(j)];
            add_block('sps_lib/Power Electronics/Universal Bridge', hb_name);
            set_param(hb_name, 'Position', [200+120*(j-1) 50+100*(i-1) 280+120*(j-1) 130+100*(i-1)]);
            set_param(hb_name, 'Arms', '2');
            set_param(hb_name, 'Device', 'IGBT / Diodes');
            set_param(hb_name, 'SnubberResistance', 'inf');
            
            % 直流侧电容
            cap_name = [subsys '/C_' phases{i} num2str(j)];
            add_block('sps_lib/Passives/Series RLC Branch', cap_name);
            set_param(cap_name, 'Position', [240+120*(j-1) 160+100*(i-1) 260+120*(j-1) 200+100*(i-1)]);
            set_param(cap_name, 'BranchType', 'C');
            set_param(cap_name, 'Capacitance', '2e-3');
        end
        
        % 输出端口
        add_block('built-in/Outport', [subsys '/Phase_' phases{i} '_out']);
        set_param([subsys '/Phase_' phases{i} '_out'], 'Position', [1500 50+100*(i-1) 1530 80+100*(i-1)]);
    end
    
    % 中性点
    add_block('sps_lib/Connectors/Neutral (input)', [subsys '/Neutral']);
    set_param([subsys '/Neutral'], 'Position', [800 400 830 430]);
end

%% 子函数：创建储能系统
function create_energy_storage_system(model_name)
    load('SVG_Parameters.mat');
    
    % 创建子系统
    add_block('built-in/Subsystem', [model_name '/Energy_Storage_System']);
    set_param([model_name '/Energy_Storage_System'], 'Position', [400 350 600 450]);
    
    subsys = [model_name '/Energy_Storage_System'];
    
    % 为每个H桥创建超级电容和DC/DC
    for phase = 1:3
        for bridge = 1:N_bridge
            % 超级电容
            sc_name = [subsys '/SC_' num2str(phase) '_' num2str(bridge)];
            add_block('sps_lib/Sources/DC Voltage Source', sc_name);
            set_param(sc_name, 'Position', [100+200*(phase-1) 50+60*(bridge-1) 150+200*(phase-1) 80+60*(bridge-1)]);
            set_param(sc_name, 'Voltage', num2str(Vsc_rated*SOC_init));
            
            % 隔离型DC/DC（简化模型）
            dcdc_name = [subsys '/DCDC_' num2str(phase) '_' num2str(bridge)];
            add_block('sps_lib/Power Electronics/Buck Converter', dcdc_name);
            set_param(dcdc_name, 'Position', [200+200*(phase-1) 50+60*(bridge-1) 300+200*(phase-1) 100+60*(bridge-1)]);
        end
    end
end

%% 子函数：创建汽车厂负载
function create_automotive_factory_load(model_name)
    load('SVG_Parameters.mat');
    
    % 创建子系统
    add_block('built-in/Subsystem', [model_name '/Automotive_Factory_Load']);
    set_param([model_name '/Automotive_Factory_Load'], 'Position', [700 150 900 250]);
    
    subsys = [model_name '/Automotive_Factory_Load'];
    
    % 工业负载（60%）- 感应电机等
    add_block('sps_lib/Loads/Three-Phase Dynamic Load', [subsys '/Industrial_Load']);
    set_param([subsys '/Industrial_Load'], 'Position', [100 50 200 100]);
    set_param([subsys '/Industrial_Load'], 'NominalVoltage', num2str(Vgrid_LL));
    set_param([subsys '/Industrial_Load'], 'ActivePower', num2str(P_industrial));
    
    % 商业负载（26%）- 照明、空调等
    add_block('sps_lib/Loads/Three-Phase Series RLC Load', [subsys '/Commercial_Load']);
    set_param([subsys '/Commercial_Load'], 'Position', [100 150 200 200]);
    set_param([subsys '/Commercial_Load'], 'NominalVoltage', num2str(Vgrid_LL));
    set_param([subsys '/Commercial_Load'], 'ActivePower', num2str(P_commercial));
    
    % 居民负载（14%）- 办公区域
    add_block('sps_lib/Loads/Three-Phase Series RLC Load', [subsys '/Residential_Load']);
    set_param([subsys '/Residential_Load'], 'Position', [100 250 200 300]);
    set_param([subsys '/Residential_Load'], 'NominalVoltage', num2str(Vgrid_LL));
    set_param([subsys '/Residential_Load'], 'ActivePower', num2str(P_residential));
    
    % 非线性负载（整流器负载）
    add_block('sps_lib/Power Electronics/Three-Phase Rectifier', [subsys '/Nonlinear_Load']);
    set_param([subsys '/Nonlinear_Load'], 'Position', [300 150 400 200]);
end

%% 子函数：创建测量系统
function create_measurement_system(model_name)
    % 创建子系统
    add_block('built-in/Subsystem', [model_name '/Measurement_System']);
    set_param([model_name '/Measurement_System'], 'Position', [700 300 900 400]);
    
    subsys = [model_name '/Measurement_System'];
    
    % 电网电压测量
    add_block('sps_lib/Measurements/Three-Phase V-I Measurement', [subsys '/Grid_Measurement']);
    set_param([subsys '/Grid_Measurement'], 'Position', [100 50 200 100]);
    
    % SVG输出测量
    add_block('sps_lib/Measurements/Three-Phase V-I Measurement', [subsys '/SVG_Measurement']);
    set_param([subsys '/SVG_Measurement'], 'Position', [100 150 200 200]);
    
    % 负载测量
    add_block('sps_lib/Measurements/Three-Phase V-I Measurement', [subsys '/Load_Measurement']);
    set_param([subsys '/Load_Measurement'], 'Position', [100 250 200 300]);
    
    % THD分析
    add_block('sps_lib/Measurements/THD', [subsys '/THD_Analysis']);
    set_param([subsys '/THD_Analysis'], 'Position', [300 150 400 200]);
    
    % 功率计算
    add_block('sps_lib/Measurements/Three-Phase Instantaneous Power', [subsys '/Power_Calc']);
    set_param([subsys '/Power_Calc'], 'Position', [300 250 400 300]);
end

%% 子函数：创建控制系统
function create_control_system(model_name)
    % 将在单独的文件中详细实现
    add_block('built-in/Subsystem', [model_name '/Control_System']);
    set_param([model_name '/Control_System'], 'Position', [500 500 700 700]);
end

%% 子函数：创建SCR检测和显示
function create_scr_detection(model_name)
    % 创建子系统
    add_block('built-in/Subsystem', [model_name '/SCR_Detection']);
    set_param([model_name '/SCR_Detection'], 'Position', [300 500 500 600]);
    
    subsys = [model_name '/SCR_Detection'];
    
    % SCR计算模块
    add_block('built-in/Fcn', [subsys '/SCR_Calc']);
    set_param([subsys '/SCR_Calc'], 'Position', [100 50 200 80]);
    set_param([subsys '/SCR_Calc'], 'Expr', 'u(1)/u(2)');
    
    % 强弱电网判断
    add_block('built-in/RelationalOperator', [subsys '/Grid_Type']);
    set_param([subsys '/Grid_Type'], 'Position', [300 50 350 80]);
    set_param([subsys '/Grid_Type'], 'Operator', '>');
    
    % 阈值常数
    add_block('built-in/Constant', [subsys '/SCR_Threshold']);
    set_param([subsys '/SCR_Threshold'], 'Position', [200 100 250 130]);
    set_param([subsys '/SCR_Threshold'], 'Value', num2str(SCR_threshold));
    
    % 显示
    add_block('built-in/Display', [subsys '/SCR_Display']);
    set_param([subsys '/SCR_Display'], 'Position', [400 50 480 80]);
    
    % Scope显示
    add_block('built-in/Scope', [subsys '/SCR_Scope']);
    set_param([subsys '/SCR_Scope'], 'Position', [400 100 480 140]);
end